{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "69ba71da",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e25c681c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4d1078ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 假设我们有一个 100 行、n=3 特征 的数据集\n",
    "np.random.seed(42)\n",
    "n_samples = 100\n",
    "n_features = 3\n",
    "\n",
    "# 生成随机数\n",
    "data = np.random.randn(n_samples, n_features)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c53f1415",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(100, 3)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "19b7116c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.09176598, -0.18323331,  0.07482166])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(data,axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6665e239",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3,)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(data,axis=0).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "70f638f9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(100, 3)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9673bc6b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8710a8d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 对于两个变量X,Y，协方差 Cov(X,Y)\n",
    "# 如果有n个特征，协方差矩阵就是对所有特征对所有特征俩俩计算这个值\n",
    "def compute_covariance_manual(data):\n",
    "    \"\"\"\n",
    "    data: numpy.ndarray, shape= (n_sample, n_features)\n",
    "    return: 协方差矩阵, shape= (n_features, n_features)\n",
    "    \"\"\"\n",
    "    n_samples, n_features = data.shape\n",
    "    \n",
    "    #1. 计算每个特征的均值\n",
    "    means = np.mean(data,axis=0)\n",
    "    #2. 去中心化（X - mean）\n",
    "    centered_data = data - means\n",
    "    \n",
    "    # XT x X\n",
    "    return centered_data.T.dot(centered_data) / (n_samples - 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "9e6c20f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.68029352, -0.03927641, -0.1108006 ],\n",
       "       [-0.03927641,  0.9586369 , -0.1348594 ],\n",
       "       [-0.1108006 , -0.1348594 ,  1.23856849]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "compute_covariance_manual(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1377ee4f",
   "metadata": {},
   "outputs": [],
   "source": [
    "seq = [7, 2, 3, 7, 5, 6, 0, 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fe099971",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[7, 3, 5, 0]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "seq[::2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "afa42b0e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# slicing\n",
    "seq[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "67e0372a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 7, 5, 6, 0, 1]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "seq[1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b43ba4e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[7, 2, 3]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "seq[:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "006e2d72",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "seq[-1:-2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b7748c4b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "seq[-2:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f59a29e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[7, 2, 3, 7, 5, 6, 0]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "seq[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8e45e8ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 0, 6, 5, 7, 3, 2, 7]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "seq[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5ea74937",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "3b4c7d75",
   "metadata": {},
   "outputs": [],
   "source": [
    "X = np.array([\n",
    "    [18, 2000, 10, 0],\n",
    "    [22, 2500,  8, 0],\n",
    "    [25, 3000,  7, 0],\n",
    "    [30, 4000,  6, 0],\n",
    "    [40, 6000,  4, 1],\n",
    "    [50, 8000,  3, 1],\n",
    "    [60,10000,  2, 1],\n",
    "    [70,12000,  1, 1],\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2a4daa53",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(8, 4)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "4d8027e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[   18,  2000,    10],\n",
       "       [   22,  2500,     8],\n",
       "       [   25,  3000,     7],\n",
       "       [   30,  4000,     6],\n",
       "       [   40,  6000,     4],\n",
       "       [   50,  8000,     3],\n",
       "       [   60, 10000,     2],\n",
       "       [   70, 12000,     1]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[:,:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "b791228e",
   "metadata": {},
   "outputs": [],
   "source": [
    "features = X[:,:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "f0c4f797",
   "metadata": {},
   "outputs": [],
   "source": [
    "target = X[:,-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "9ff87473",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_centered = features - features.mean(axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "82b761ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2.1375e+01, -3.9375e+03,  4.8750e+00],\n",
       "       [-1.7375e+01, -3.4375e+03,  2.8750e+00],\n",
       "       [-1.4375e+01, -2.9375e+03,  1.8750e+00],\n",
       "       [-9.3750e+00, -1.9375e+03,  8.7500e-01],\n",
       "       [ 6.2500e-01,  6.2500e+01, -1.1250e+00],\n",
       "       [ 1.0625e+01,  2.0625e+03, -2.1250e+00],\n",
       "       [ 2.0625e+01,  4.0625e+03, -3.1250e+00],\n",
       "       [ 3.0625e+01,  6.0625e+03, -4.1250e+00]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_centered"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "13aef151",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2.1375e+01, -1.7375e+01, -1.4375e+01, -9.3750e+00,  6.2500e-01,\n",
       "         1.0625e+01,  2.0625e+01,  3.0625e+01],\n",
       "       [-3.9375e+03, -3.4375e+03, -2.9375e+03, -1.9375e+03,  6.2500e+01,\n",
       "         2.0625e+03,  4.0625e+03,  6.0625e+03],\n",
       "       [ 4.8750e+00,  2.8750e+00,  1.8750e+00,  8.7500e-01, -1.1250e+00,\n",
       "        -2.1250e+00, -3.1250e+00, -4.1250e+00]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_centered.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "563da482",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "ccedc9d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "y_centered = target - target.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "ac862dc9",
   "metadata": {},
   "outputs": [],
   "source": [
    "numerator = X_centered.T @ y_centered"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "af225017",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6.250e+01,  1.225e+04, -1.050e+01])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numerator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c483ca7d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "cd675c81",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.5, -0.5, -0.5, -0.5,  0.5,  0.5,  0.5,  0.5])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_centered"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "2f03b583",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2.1375e+01, -3.9375e+03,  4.8750e+00],\n",
       "       [-1.7375e+01, -3.4375e+03,  2.8750e+00],\n",
       "       [-1.4375e+01, -2.9375e+03,  1.8750e+00],\n",
       "       [-9.3750e+00, -1.9375e+03,  8.7500e-01],\n",
       "       [ 6.2500e-01,  6.2500e+01, -1.1250e+00],\n",
       "       [ 1.0625e+01,  2.0625e+03, -2.1250e+00],\n",
       "       [ 2.0625e+01,  4.0625e+03, -3.1250e+00],\n",
       "       [ 3.0625e+01,  6.0625e+03, -4.1250e+00]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_centered"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "1354e94d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.5, -0.5, -0.5, -0.5,  0.5,  0.5,  0.5,  0.5])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_centered"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "08772ca8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9860.088678607308"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sqrt(np.sum(X_centered **2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "1c25e390",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(X_centered)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "494b3870",
   "metadata": {},
   "outputs": [],
   "source": [
    "X = np.array([\n",
    "    [18, 2000, 10, 0],\n",
    "    [22, 2500,  8, 0],\n",
    "    [25, 3000,  7, 0],\n",
    "    [30, 4000,  6, 0],\n",
    "    [40, 6000,  4, 1],\n",
    "    [50, 8000,  3, 1],\n",
    "    [60,10000,  2, 1],\n",
    "    [70,12000,  1, 1],\n",
    "])\n",
    "\n",
    "features = X[:, :-1]\n",
    "target   = X[:, -1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "eeee1d54",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[   18,  2000,    10],\n",
       "        [   22,  2500,     8],\n",
       "        [   25,  3000,     7],\n",
       "        [   30,  4000,     6],\n",
       "        [   40,  6000,     4],\n",
       "        [   50,  8000,     3],\n",
       "        [   60, 10000,     2],\n",
       "        [   70, 12000,     1]]),\n",
       " array([0, 0, 0, 0, 1, 1, 1, 1]))"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features,target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "31771efd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([18, 22, 25, 30, 40, 50, 60, 70])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features[:,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "e234b39d",
   "metadata": {},
   "outputs": [],
   "source": [
    "points = np.arange(-5, 5, 0.01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "9984bc9c",
   "metadata": {},
   "outputs": [],
   "source": [
    "xs, ys = np.meshgrid(points, points)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "6951d524",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((1000, 1000), (1000, 1000))"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xs.shape,ys.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "5ddad65c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-4.99"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xs[0,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "b67b4470",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1000, 1000)"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xs.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "c70a3249",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 采用循环的方式计算每个属性向量与target属性向量的的peason correlation\n",
    "X =features[:,0]\n",
    "y = target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "2a5f986b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([18, 22, 25, 30, 40, 50, 60, 70]), array([0, 0, 0, 0, 1, 1, 1, 1]))"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X,y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "a048ca5c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ddd6cca7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7974abb3",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "9b5131e8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[   18,  2000,    10],\n",
       "       [   22,  2500,     8],\n",
       "       [   25,  3000,     7],\n",
       "       [   30,  4000,     6],\n",
       "       [   40,  6000,     4],\n",
       "       [   50,  8000,     3],\n",
       "       [   60, 10000,     2],\n",
       "       [   70, 12000,     1]])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "d9b96e6a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8786491390204952"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算一个特征与目标的相关性\n",
    "X=features[:,0]\n",
    "y=target\n",
    "\n",
    "x_mean = np.mean(X)\n",
    "y_mean = np.mean(y)\n",
    "\n",
    "numerator = np.sum((X-x_mean) * (y-y_mean))\n",
    "denominator = np.sqrt(np.sum((X-x_mean) ** 2)) * np.sqrt(np.sum((y-y_mean) **2))\n",
    "\n",
    "numerator / denominator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "2a9b8521",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 一次性计算所有特征与目标的相关性\n",
    "X = np.array([\n",
    "    [18, 2000, 10, 0],\n",
    "    [22, 2500,  8, 0],\n",
    "    [25, 3000,  7, 0],\n",
    "    [30, 4000,  6, 0],\n",
    "    [40, 6000,  4, 1],\n",
    "    [50, 8000,  3, 1],\n",
    "    [60,10000,  2, 1],\n",
    "    [70,12000,  1, 1],\n",
    "])\n",
    "features = X[:,:-1]\n",
    "target = X[:,-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "880e1b5e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[   18,  2000,    10],\n",
       "       [   22,  2500,     8],\n",
       "       [   25,  3000,     7],\n",
       "       [   30,  4000,     6],\n",
       "       [   40,  6000,     4],\n",
       "       [   50,  8000,     3],\n",
       "       [   60, 10000,     2],\n",
       "       [   70, 12000,     1]])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "6000ed6e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 1, 1, 1, 1])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "b66372d7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3.9375e+01, 5.9375e+03, 5.1250e+00])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features.mean(axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "8e1cdfff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2.1375e+01, -3.9375e+03,  4.8750e+00],\n",
       "       [-1.7375e+01, -3.4375e+03,  2.8750e+00],\n",
       "       [-1.4375e+01, -2.9375e+03,  1.8750e+00],\n",
       "       [-9.3750e+00, -1.9375e+03,  8.7500e-01],\n",
       "       [ 6.2500e-01,  6.2500e+01, -1.1250e+00],\n",
       "       [ 1.0625e+01,  2.0625e+03, -2.1250e+00],\n",
       "       [ 2.0625e+01,  4.0625e+03, -3.1250e+00],\n",
       "       [ 3.0625e+01,  6.0625e+03, -4.1250e+00]])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features - features.mean(axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "96705053",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[   18,    22,    25,    30,    40,    50,    60,    70],\n",
       "       [ 2000,  2500,  3000,  4000,  6000,  8000, 10000, 12000],\n",
       "       [   10,     8,     7,     6,     4,     3,     2,     1]])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "783f6562",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3.9375e+01, 5.9375e+03, 5.1250e+00])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features.T.mean(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "46d4a54e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[   18,    22,    25,    30,    40,    50,    60,    70],\n",
       "       [ 2000,  2500,  3000,  4000,  6000,  8000, 10000, 12000],\n",
       "       [   10,     8,     7,     6,     4,     3,     2,     1]])"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "3582208e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 676.        ,  843.33333333, 1010.66666667, 1345.33333333,\n",
       "       2014.66666667, 2684.33333333, 3354.        , 4023.66666667])"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features.T.mean(axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "b4fa67f9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[   18,  2000,    10],\n",
       "       [   22,  2500,     8],\n",
       "       [   25,  3000,     7],\n",
       "       [   30,  4000,     6],\n",
       "       [   40,  6000,     4],\n",
       "       [   50,  8000,     3],\n",
       "       [   60, 10000,     2],\n",
       "       [   70, 12000,     1]])"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "2f69cc8c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(features)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "394ed5d7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(8, 3)"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "87fb46ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([18, 22, 25, 30, 40, 50, 60, 70])"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features[:,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "aa318443",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(features[:,0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "5306c557",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(8,)"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features[:,0].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "43547c7c",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_centered = features - np.mean(features,axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "a8bd40c7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2.1375e+01, -3.9375e+03,  4.8750e+00],\n",
       "       [-1.7375e+01, -3.4375e+03,  2.8750e+00],\n",
       "       [-1.4375e+01, -2.9375e+03,  1.8750e+00],\n",
       "       [-9.3750e+00, -1.9375e+03,  8.7500e-01],\n",
       "       [ 6.2500e-01,  6.2500e+01, -1.1250e+00],\n",
       "       [ 1.0625e+01,  2.0625e+03, -2.1250e+00],\n",
       "       [ 2.0625e+01,  4.0625e+03, -3.1250e+00],\n",
       "       [ 3.0625e+01,  6.0625e+03, -4.1250e+00]])"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_centered"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "24e17a39",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2.1375e+01, -3.9375e+03,  4.8750e+00],\n",
       "       [-1.7375e+01, -3.4375e+03,  2.8750e+00],\n",
       "       [-1.4375e+01, -2.9375e+03,  1.8750e+00],\n",
       "       [-9.3750e+00, -1.9375e+03,  8.7500e-01],\n",
       "       [ 6.2500e-01,  6.2500e+01, -1.1250e+00],\n",
       "       [ 1.0625e+01,  2.0625e+03, -2.1250e+00],\n",
       "       [ 2.0625e+01,  4.0625e+03, -3.1250e+00],\n",
       "       [ 3.0625e+01,  6.0625e+03, -4.1250e+00]])"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "features - features.mean(axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "e3d43ffd",
   "metadata": {},
   "outputs": [],
   "source": [
    "y_centered = target - target.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "191eda1c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[-2.1375e+01, -3.9375e+03,  4.8750e+00],\n",
       "        [-1.7375e+01, -3.4375e+03,  2.8750e+00],\n",
       "        [-1.4375e+01, -2.9375e+03,  1.8750e+00],\n",
       "        [-9.3750e+00, -1.9375e+03,  8.7500e-01],\n",
       "        [ 6.2500e-01,  6.2500e+01, -1.1250e+00],\n",
       "        [ 1.0625e+01,  2.0625e+03, -2.1250e+00],\n",
       "        [ 2.0625e+01,  4.0625e+03, -3.1250e+00],\n",
       "        [ 3.0625e+01,  6.0625e+03, -4.1250e+00]]),\n",
       " array([-0.5, -0.5, -0.5, -0.5,  0.5,  0.5,  0.5,  0.5]))"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_centered,y_centered"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "3e71577f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2.1375e+01, -1.7375e+01, -1.4375e+01, -9.3750e+00,  6.2500e-01,\n",
       "         1.0625e+01,  2.0625e+01,  3.0625e+01],\n",
       "       [-3.9375e+03, -3.4375e+03, -2.9375e+03, -1.9375e+03,  6.2500e+01,\n",
       "         2.0625e+03,  4.0625e+03,  6.0625e+03],\n",
       "       [ 4.8750e+00,  2.8750e+00,  1.8750e+00,  8.7500e-01, -1.1250e+00,\n",
       "        -2.1250e+00, -3.1250e+00, -4.1250e+00]])"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 分子：每个特征和y的协方差\n",
    "X_centered.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "e6b3ab66",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 8)"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_centered.T.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "49df1728",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(8,)"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "be3648ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6.250e+01,  1.225e+04, -1.050e+01])"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_centered.T @ y_centered"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "a0300c78",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 8 is different from 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[83], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mX_centered\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m@\u001b[39;49m\u001b[43m \u001b[49m\u001b[43my_centered\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mT\u001b[49m\n",
      "\u001b[1;31mValueError\u001b[0m: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 8 is different from 3)"
     ]
    }
   ],
   "source": [
    "X_centered @ y_centered.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "71ae1eee",
   "metadata": {},
   "outputs": [],
   "source": [
    "vector_a = np.array([1, 2, 3, 4])      # 4个样本\n",
    "vector_b = np.array([5, 6, 7])         # 3个样本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "2bd227ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 两个样本数不同的向量\n",
    "vector_a = np.array([1, 2, 3, 4])    # 4个样本\n",
    "vector_b = np.array([5, 6, 7])       # 3个样本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "953cc073",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndentationError",
     "evalue": "unexpected indent (1299885106.py, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn[87], line 2\u001b[1;36m\u001b[0m\n\u001b[1;33m    print(f\"向量a: {a}, 形状: {a.shape}\")\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m unexpected indent\n"
     ]
    }
   ],
   "source": [
    "\"\"\"可视化向量乘法的维度解释\"\"\"\n",
    "    print(f\"向量a: {a}, 形状: {a.shape}\")\n",
    "    print(f\"向量b: {b}, 形状: {b.shape}\")\n",
    "    \n",
    "    # NumPy如何解释这些向量\n",
    "    print(\"\\nNumPy自动解释:\")\n",
    "    print(f\"a (形状{a.shape}) → 行向量: (1, {len(a)})\")\n",
    "    print(f\"b (形状{b.shape}) → 列向量: ({len(b)}, 1)\")\n",
    "    \n",
    "    print(f\"\\n尝试计算: (1, {len(a)}) @ ({len(b)}, 1)\")\n",
    "    print(f\"维度要求: {len(a)} == {len(b)}\")\n",
    "    print(f\"实际: {len(a)} ≠ {len(b)} → 错误！\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "7a8c9674",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndentationError",
     "evalue": "unexpected indent (3453292397.py, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn[88], line 2\u001b[1;36m\u001b[0m\n\u001b[1;33m    print(f\"向量a: a, 形状: a.shape\")\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m unexpected indent\n"
     ]
    }
   ],
   "source": [
    "\"\"\"可视化向量乘法的维度解释\"\"\"\n",
    "    print(f\"向量a: a, 形状: a.shape\")\n",
    "    print(f\"向量b: b, 形状: b.shape\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "e92cee2b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 两个样本数不同的向量\n",
    "a = np.array([1, 2, 3, 4])    # 4个样本\n",
    "b = np.array([5, 6, 7])       # 3个样本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "76054755",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndentationError",
     "evalue": "unexpected indent (1299885106.py, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn[90], line 2\u001b[1;36m\u001b[0m\n\u001b[1;33m    print(f\"向量a: {a}, 形状: {a.shape}\")\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m unexpected indent\n"
     ]
    }
   ],
   "source": [
    "\"\"\"可视化向量乘法的维度解释\"\"\"\n",
    "    print(f\"向量a: {a}, 形状: {a.shape}\")\n",
    "    print(f\"向量b: {b}, 形状: {b.shape}\")\n",
    "    \n",
    "    # NumPy如何解释这些向量\n",
    "    print(\"\\nNumPy自动解释:\")\n",
    "    print(f\"a (形状{a.shape}) → 行向量: (1, {len(a)})\")\n",
    "    print(f\"b (形状{b.shape}) → 列向量: ({len(b)}, 1)\")\n",
    "    \n",
    "    print(f\"\\n尝试计算: (1, {len(a)}) @ ({len(b)}, 1)\")\n",
    "    print(f\"维度要求: {len(a)} == {len(b)}\")\n",
    "    print(f\"实际: {len(a)} ≠ {len(b)} → 错误！\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "40d26541",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((4,), (3,))"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.shape,b.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "c2f6dde9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "7807ecc4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 6, 7])"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "6d2210e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "b8c718e0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1])"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0:1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "d96cc2da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "2a785ed6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 数据\n",
    "X = np.array([\n",
    "    [18, 2000, 10, 0],\n",
    "    [22, 2500,  8, 0],\n",
    "    [25, 3000,  7, 0],\n",
    "    [30, 4000,  6, 0],\n",
    "    [40, 6000,  4, 1],\n",
    "    [50, 8000,  3, 1],\n",
    "    [60,10000,  2, 1],\n",
    "    [70,12000,  1, 1],\n",
    "])\n",
    "\n",
    "features = X[:, :-1]   # age, income, exercise_hours\n",
    "target   = X[:, -1]    # has_disease\n",
    "\n",
    "# 中心化\n",
    "X_centered = features - features.mean(axis=0)\n",
    "y_centered = target - target.mean()\n",
    "\n",
    "# 分子：每个特征和 y 的协方差\n",
    "numerator = X_centered.T @ y_centered\n",
    "\n",
    "# 分母：特征标准差 × y标准差\n",
    "denominator = np.sqrt(np.sum(X_centered**2, axis=0)) * np.sqrt(np.sum(y_centered**2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "1f9447ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6.250e+01,  1.225e+04, -1.050e+01])"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numerator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "1c3f89be",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([7.11319197e+01, 1.39440848e+04, 1.17366946e+01])"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "denominator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "a0fec9d2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.87864914,  0.87850872, -0.89463008])"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numerator / denominator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "75b4b03e",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "age 与 has_disease 的相关性: 0.879\n",
      "income 与 has_disease 的相关性: 0.879\n",
      "exercise_hours 与 has_disease 的相关性: -0.895\n"
     ]
    }
   ],
   "source": [
    "# 相关性\n",
    "correlations = numerator / denominator\n",
    "\n",
    "# 打印结果\n",
    "for name, corr in zip([\"age\", \"income\", \"exercise_hours\"], correlations):\n",
    "    print(f\"{name} 与 has_disease 的相关性: {corr:.3f}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b9be4577",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c06a0236",
   "metadata": {},
   "outputs": [],
   "source": [
    "shuffled_indices = np.random.permutation(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a4d8bc13",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([80, 15, 16, 61, 74, 66, 93, 20, 63, 83, 75,  5, 35, 46, 64, 71, 26,\n",
       "       45, 52, 91, 28, 97, 73, 11,  0, 62, 76, 70, 30, 18,  2, 56, 92, 43,\n",
       "       24, 95, 44, 50, 86, 37, 58, 85, 32, 78, 48, 13, 21,  7, 25, 79, 40,\n",
       "       42, 49, 55, 29, 17, 31, 33, 36, 22,  8, 12, 72, 10, 99, 81, 47, 69,\n",
       "       96, 23, 65, 27, 41, 88, 57,  6, 84, 94, 67, 39, 87, 38, 82, 89,  9,\n",
       "       54, 51, 77, 98, 53,  3, 60,  4, 19, 14,  1, 34, 59, 90, 68])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shuffled_indices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e0e532e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([80, 15, 16, 61, 74, 66, 93, 20, 63, 83, 75,  5, 35, 46, 64, 71, 26,\n",
       "       45, 52, 91, 28, 97, 73, 11,  0, 62, 76, 70, 30, 18,  2, 56, 92, 43,\n",
       "       24, 95, 44, 50, 86, 37, 58, 85, 32, 78, 48, 13, 21,  7, 25, 79, 40,\n",
       "       42, 49, 55, 29, 17, 31, 33, 36, 22,  8, 12, 72, 10, 99, 81, 47, 69,\n",
       "       96, 23, 65, 27, 41, 88, 57,  6, 84, 94, 67, 39, 87, 38, 82, 89,  9,\n",
       "       54, 51, 77, 98, 53,  3, 60,  4, 19, 14,  1, 34, 59, 90, 68])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shuffled_indices[:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4aa49723",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([80, 15, 16, 61, 74, 66, 93, 20, 63, 83, 75,  5, 35, 46, 64, 71, 26,\n",
       "       45, 52, 91, 28, 97, 73, 11,  0, 62, 76, 70, 30, 18,  2, 56, 92, 43,\n",
       "       24, 95, 44, 50, 86, 37, 58, 85, 32, 78, 48, 13, 21,  7, 25, 79, 40,\n",
       "       42, 49, 55, 29, 17, 31, 33, 36, 22,  8, 12, 72, 10, 99, 81, 47, 69,\n",
       "       96, 23])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shuffled_indices[:70]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "58edb63f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([73, 11,  0, 62, 76, 70, 30, 18,  2, 56, 92, 43, 24, 95, 44, 50, 86,\n",
       "       37, 58, 85, 32, 78, 48, 13, 21,  7, 25, 79, 40, 42, 49, 55, 29, 17,\n",
       "       31, 33, 36, 22,  8, 12, 72, 10, 99, 81, 47, 69, 96, 23])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shuffled_indices[22:70]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "32f44cbb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6f869c57",
   "metadata": {},
   "outputs": [],
   "source": [
    "s1 = pd.Series([100, 200, 300], index=[10,20,30])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "09b19b02",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10    100\n",
       "20    200\n",
       "30    300\n",
       "dtype: int64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2de22500",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1.iloc[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "2e853d98",
   "metadata": {},
   "outputs": [],
   "source": [
    "s2 = pd.Series([100, 200, 300], index = ['a','b','c'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c9d0572b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    100\n",
       "b    200\n",
       "c    300\n",
       "dtype: int64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "8a6fe838",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(100, 300)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2.iloc[0],s2.iloc[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4dae7060",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn.model_selection import StratifiedShuffleSplit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "382f72d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "X = np.array([[1,2],[3,4],[1,2],[3,4],[1,2],[3,4]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c6bca9b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "y = np.array([0,0,0,1,1,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a6f0dd86",
   "metadata": {},
   "outputs": [],
   "source": [
    "sss = StratifiedShuffleSplit(n_splits=5, test_size=0.5, random_state=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c33b99e8",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fold 0\n",
      "  Train: index=[5 2 3]\n",
      "  Test:  index=[4 1 0]\n",
      "Fold 1\n",
      "  Train: index=[5 1 4]\n",
      "  Test:  index=[0 2 3]\n",
      "Fold 2\n",
      "  Train: index=[5 0 2]\n",
      "  Test:  index=[4 3 1]\n",
      "Fold 3\n",
      "  Train: index=[4 1 0]\n",
      "  Test:  index=[2 3 5]\n",
      "Fold 4\n",
      "  Train: index=[0 5 1]\n",
      "  Test:  index=[3 4 2]\n"
     ]
    }
   ],
   "source": [
    "for i,(train_index, test_index) in enumerate(sss.split(X,y)):\n",
    "    print(f\"Fold {i}\")\n",
    "    print(f\"  Train: index={train_index}\")\n",
    "    print(f\"  Test:  index={test_index}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ea9a948f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import KFold"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "bca6876e",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3] [0]\n",
      "[0 2 3] [1]\n",
      "[0 1 3] [2]\n",
      "[0 1 2] [3]\n"
     ]
    }
   ],
   "source": [
    "X = [\"a\",\"b\",\"c\",\"d\"]\n",
    "kf = KFold(n_splits=4) # 将数据分成4折（4份）\n",
    "for train, test in kf.split(X):\n",
    "    print(\"%s %s\" % (train, test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "21e87b01",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# stratified: the folds are made by preserving the percentage of samples for each label.\n",
    "from sklearn.model_selection import StratifiedKFold\n",
    "X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]])\n",
    "y = np.array([0, 0, 1, 1])\n",
    "skf = StratifiedKFold(n_splits=2)\n",
    "skf.get_n_splits(X,y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "6463f7eb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fold 0\n",
      "  Train: index=[1 3]\n",
      "  Test:  index=[0 2]\n",
      "Fold 1\n",
      "  Train: index=[0 2]\n",
      "  Test:  index=[1 3]\n"
     ]
    }
   ],
   "source": [
    "for i, (train_index, test_index) in enumerate(skf.split(X,y)):\n",
    "    print(f\"Fold {i}\")\n",
    "    print(f\"  Train: index={train_index}\")\n",
    "    print(f\"  Test:  index={test_index}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "511ba3ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "937dc6e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 4],\n",
       "       [2, 5],\n",
       "       [3, 6]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.c_[np.array([1,2,3]), np.array([4,5,6])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4f673fba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0],\n",
       "       [1],\n",
       "       [2],\n",
       "       [3],\n",
       "       [4]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.c_[0:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "bc00e134",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.r_[0:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "849e1835",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  1,  2,  3],\n",
       "        [ 4,  5,  6,  7]],\n",
       "\n",
       "       [[ 8,  9, 10, 11],\n",
       "        [12, 13, 14, 15]]])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.arange(16).reshape((2,2,4))\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c26a1278",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 2, 4)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c8e0a180",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4, 2, 2)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.transpose().shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4a68bcec",
   "metadata": {},
   "outputs": [],
   "source": [
    "tup = (2, 2, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "30acd60f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 2, 4)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0da8eaae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4, 2, 2)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tup[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a8b94263",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
